package com.example.work.controller;

import com.example.work.entity.TbCheckAreaItem;
import com.example.work.entity.TbCheckItemScore;
import com.example.work.service.IAreaItemService;
import com.example.work.service.ITbCheckItemScoreService;
import com.example.work.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/score/areaItem")
public class AreaItemScoreController {

    @Autowired
    private ITbCheckItemScoreService itemScoreService;

    @Autowired
    private IAreaItemService itemService;

    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public R addareaItemScore(TbCheckItemScore checkItemScore){
        try{
            // 获取项目信息的id
            TbCheckAreaItem item = itemService.getById(checkItemScore.getItemId());
            if(null == item)
                throw new Exception();
            // 填补数据
            checkItemScore.setDeficiencyScore(item.getDeficiencyScore());
            checkItemScore.setScore(item.getScore());

            if(!itemScoreService.save(checkItemScore))
                throw new Exception();
            return R.ok("新增打分项目成功");
        }catch (Exception e){
            e.printStackTrace();
            return R.error("新增打分项目失败：" + e.getMessage());
        }
    }
}
